<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>basestore.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_gears_basestore.js.html">basestore.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2006 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Definition of goog.gears.BaseStore which
<a name="line17"></a> * is a base class for the various database stores. It provides
<a name="line18"></a> * the basic structure for creating, updating and removing the store, as well
<a name="line19"></a> * as versioning. It also provides ways to interconnect stores.
<a name="line20"></a> *
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.gears.BaseStore&#39;);
<a name="line24"></a>goog.provide(&#39;goog.gears.BaseStore.SchemaType&#39;);
<a name="line25"></a>
<a name="line26"></a>goog.require(&#39;goog.Disposable&#39;);
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>/**
<a name="line30"></a> * This class implements the common store functionality
<a name="line31"></a> *
<a name="line32"></a> * @param {goog.gears.Database} database The data base to store the data in.
<a name="line33"></a> * @constructor
<a name="line34"></a> * @extends {goog.Disposable}
<a name="line35"></a> */
<a name="line36"></a>goog.gears.BaseStore = function(database) {
<a name="line37"></a>  goog.Disposable.call(this);
<a name="line38"></a>
<a name="line39"></a>  /**
<a name="line40"></a>   * The underlying database that holds the message store.
<a name="line41"></a>   * @private
<a name="line42"></a>   * @type {goog.gears.Database}
<a name="line43"></a>   */
<a name="line44"></a>  this.database_ = database;
<a name="line45"></a>};
<a name="line46"></a>goog.inherits(goog.gears.BaseStore, goog.Disposable);
<a name="line47"></a>
<a name="line48"></a>
<a name="line49"></a>/**
<a name="line50"></a> * Schema definition types
<a name="line51"></a> * @enum {number}
<a name="line52"></a> */
<a name="line53"></a>goog.gears.BaseStore.SchemaType = {
<a name="line54"></a>  TABLE: 1,
<a name="line55"></a>  VIRTUAL_TABLE: 2,
<a name="line56"></a>  INDEX: 3,
<a name="line57"></a>  BEFORE_INSERT_TRIGGER: 4,
<a name="line58"></a>  AFTER_INSERT_TRIGGER: 5,
<a name="line59"></a>  BEFORE_UPDATE_TRIGGER: 6,
<a name="line60"></a>  AFTER_UPDATE_TRIGGER: 7,
<a name="line61"></a>  BEFORE_DELETE_TRIGGER: 8,
<a name="line62"></a>  AFTER_DELETE_TRIGGER: 9
<a name="line63"></a>};
<a name="line64"></a>
<a name="line65"></a>
<a name="line66"></a>/**
<a name="line67"></a> * The name of the store. Subclasses should override and choose their own
<a name="line68"></a> * name. That name is used for the maintaining the version string
<a name="line69"></a> * @protected
<a name="line70"></a> * @type {string}
<a name="line71"></a> */
<a name="line72"></a>goog.gears.BaseStore.prototype.name = &#39;Base&#39;;
<a name="line73"></a>
<a name="line74"></a>
<a name="line75"></a>/**
<a name="line76"></a> * The version number of the database schema. It is used to determine whether
<a name="line77"></a> * the store&#39;s portion of the database needs to be updated. Subclassses should
<a name="line78"></a> * override this value.
<a name="line79"></a> * @protected
<a name="line80"></a> * @type {number}
<a name="line81"></a> */
<a name="line82"></a>goog.gears.BaseStore.prototype.version = 1;
<a name="line83"></a>
<a name="line84"></a>
<a name="line85"></a>/**
<a name="line86"></a> * The database schema for the store. This is an array of objects, where each
<a name="line87"></a> * object describes a database object (table, index, trigger). Documentation
<a name="line88"></a> * about the object&#39;s fields can be found in the #createSchema documentation.
<a name="line89"></a> * This is in the prototype so that it can be overriden by the subclass. This
<a name="line90"></a> * field is read only.
<a name="line91"></a> * @protected
<a name="line92"></a> * @type {Array.&lt;Object&gt;}
<a name="line93"></a> */
<a name="line94"></a>goog.gears.BaseStore.prototype.schema = [];
<a name="line95"></a>
<a name="line96"></a>
<a name="line97"></a>/**
<a name="line98"></a> * Updates the tables for the message store in the case where
<a name="line99"></a> * they are out of date.
<a name="line100"></a> *
<a name="line101"></a> * @protected
<a name="line102"></a> * @param {number} persistedVersion the current version of the tables in the
<a name="line103"></a> * database.
<a name="line104"></a> */
<a name="line105"></a>goog.gears.BaseStore.prototype.updateStore = function(persistedVersion) {
<a name="line106"></a>  // TODO: Need to figure out how to handle updates
<a name="line107"></a>  // where to store the version number and is it globale or per unit.
<a name="line108"></a>};
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>/**
<a name="line112"></a> * Preloads any applicable data into the tables.
<a name="line113"></a> *
<a name="line114"></a> * @protected
<a name="line115"></a> */
<a name="line116"></a>goog.gears.BaseStore.prototype.loadData = function() {
<a name="line117"></a>};
<a name="line118"></a>
<a name="line119"></a>
<a name="line120"></a>/**
<a name="line121"></a> * Creates in memory cache of data that is stored in the tables.
<a name="line122"></a> *
<a name="line123"></a> * @protected
<a name="line124"></a> */
<a name="line125"></a>goog.gears.BaseStore.prototype.getCachedData = function() {
<a name="line126"></a>};
<a name="line127"></a>
<a name="line128"></a>
<a name="line129"></a>/**
<a name="line130"></a> * Informs other stores that this store exists .
<a name="line131"></a> *
<a name="line132"></a> * @protected
<a name="line133"></a> */
<a name="line134"></a>goog.gears.BaseStore.prototype.informOtherStores = function() {
<a name="line135"></a>};
<a name="line136"></a>
<a name="line137"></a>
<a name="line138"></a>/**
<a name="line139"></a> * Makes sure that tables needed for the store exist and are up to date.
<a name="line140"></a> */
<a name="line141"></a>goog.gears.BaseStore.prototype.ensureStoreExists = function() {
<a name="line142"></a>  var persistedVersion = this.getStoreVersion();
<a name="line143"></a>
<a name="line144"></a>  if (persistedVersion) {
<a name="line145"></a>    if (persistedVersion != this.version) {
<a name="line146"></a>      // update
<a name="line147"></a>      this.database_.begin();
<a name="line148"></a>      try {
<a name="line149"></a>        this.updateStore(persistedVersion);
<a name="line150"></a>        this.setStoreVersion_(this.version);
<a name="line151"></a>        this.database_.commit();
<a name="line152"></a>      } catch (ex) {
<a name="line153"></a>        this.database_.rollback(ex);
<a name="line154"></a>        throw Error(&#39;Could not update the &#39; + this.name + &#39; schema &#39; +
<a name="line155"></a>            &#39; from version &#39; + persistedVersion + &#39; to &#39; + this.version +
<a name="line156"></a>            &#39;: &#39; + (ex.message || &#39;unknown exception&#39;));
<a name="line157"></a>      }
<a name="line158"></a>    }
<a name="line159"></a>  } else {
<a name="line160"></a>    // create
<a name="line161"></a>    this.database_.begin();
<a name="line162"></a>    try {
<a name="line163"></a>      // This is rarely necessary, but it&#39;s possible if we rolled back a
<a name="line164"></a>      // release and dropped the schema on version n-1 before installing
<a name="line165"></a>      // again on version n.
<a name="line166"></a>      this.dropSchema(this.schema);
<a name="line167"></a>
<a name="line168"></a>      this.createSchema(this.schema);
<a name="line169"></a>
<a name="line170"></a>      // Ensure that the version info schema exists.
<a name="line171"></a>      this.createSchema([{
<a name="line172"></a>        type: goog.gears.BaseStore.SchemaType.TABLE,
<a name="line173"></a>        name: &#39;StoreVersionInfo&#39;,
<a name="line174"></a>        columns: [
<a name="line175"></a>          &#39;StoreName TEXT NOT NULL PRIMARY KEY&#39;,
<a name="line176"></a>          &#39;Version INTEGER NOT NULL&#39;
<a name="line177"></a>        ]}], true);
<a name="line178"></a>      this.loadData();
<a name="line179"></a>      this.setStoreVersion_(this.version);
<a name="line180"></a>      this.database_.commit();
<a name="line181"></a>    } catch (ex) {
<a name="line182"></a>      this.database_.rollback(ex);
<a name="line183"></a>      throw Error(&#39;Could not create the &#39; + this.name + &#39; schema&#39; +
<a name="line184"></a>            &#39;: &#39; + (ex.message || &#39;unknown exception&#39;));
<a name="line185"></a>    }
<a name="line186"></a>  }
<a name="line187"></a>  this.getCachedData();
<a name="line188"></a>  this.informOtherStores();
<a name="line189"></a>};
<a name="line190"></a>
<a name="line191"></a>
<a name="line192"></a>/**
<a name="line193"></a> * Removes the tables for the MessageStore
<a name="line194"></a> */
<a name="line195"></a>goog.gears.BaseStore.prototype.removeStore = function() {
<a name="line196"></a>  this.database_.begin();
<a name="line197"></a>  try {
<a name="line198"></a>    this.removeStoreVersion();
<a name="line199"></a>    this.dropSchema(this.schema);
<a name="line200"></a>    this.database_.commit();
<a name="line201"></a>  } catch (ex) {
<a name="line202"></a>    this.database_.rollback(ex);
<a name="line203"></a>    throw Error(&#39;Could not remove the &#39; + this.name + &#39; schema&#39; +
<a name="line204"></a>            &#39;: &#39; + (ex.message || &#39;unknown exception&#39;));
<a name="line205"></a>  }
<a name="line206"></a>};
<a name="line207"></a>
<a name="line208"></a>
<a name="line209"></a>/**
<a name="line210"></a> * Returns the name of the store.
<a name="line211"></a> *
<a name="line212"></a> * @return {string} The name of the store.
<a name="line213"></a> */
<a name="line214"></a>goog.gears.BaseStore.prototype.getName = function() {
<a name="line215"></a>  return this.name;
<a name="line216"></a>};
<a name="line217"></a>
<a name="line218"></a>
<a name="line219"></a>/**
<a name="line220"></a> * Returns the version number for the specified store
<a name="line221"></a> *
<a name="line222"></a> * @return {number} The version number of the store. Returns 0 if the
<a name="line223"></a> *     store does not exist.
<a name="line224"></a> */
<a name="line225"></a>goog.gears.BaseStore.prototype.getStoreVersion = function() {
<a name="line226"></a>  try {
<a name="line227"></a>    return /** @type {number} */ (this.database_.queryValue(
<a name="line228"></a>        &#39;SELECT Version FROM StoreVersionInfo WHERE StoreName=?&#39;,
<a name="line229"></a>        this.name)) || 0;
<a name="line230"></a>  } catch (ex) {
<a name="line231"></a>    return 0;
<a name="line232"></a>  }
<a name="line233"></a>};
<a name="line234"></a>
<a name="line235"></a>
<a name="line236"></a>/**
<a name="line237"></a> * Sets the version number for the specified store
<a name="line238"></a> *
<a name="line239"></a> * @param {number} version The version number for the store.
<a name="line240"></a> * @private
<a name="line241"></a> */
<a name="line242"></a>goog.gears.BaseStore.prototype.setStoreVersion_ = function(version) {
<a name="line243"></a>  // TODO: Need to determine if we should enforce the fact
<a name="line244"></a>  // that store versions are monotonically increasing.
<a name="line245"></a>  this.database_.execute(
<a name="line246"></a>      &#39;INSERT OR REPLACE INTO StoreVersionInfo &#39; +
<a name="line247"></a>      &#39;(StoreName, Version) VALUES(?,?)&#39;,
<a name="line248"></a>      this.name,
<a name="line249"></a>      version);
<a name="line250"></a>};
<a name="line251"></a>
<a name="line252"></a>
<a name="line253"></a>/**
<a name="line254"></a> * Removes the version number for the specified store
<a name="line255"></a> */
<a name="line256"></a>goog.gears.BaseStore.prototype.removeStoreVersion = function() {
<a name="line257"></a>  try {
<a name="line258"></a>    this.database_.execute(
<a name="line259"></a>        &#39;DELETE FROM StoreVersionInfo WHERE StoreName=?&#39;,
<a name="line260"></a>        this.name);
<a name="line261"></a>  } catch (ex) {
<a name="line262"></a>    // Ignore error - part of bootstrap process.
<a name="line263"></a>  }
<a name="line264"></a>};
<a name="line265"></a>
<a name="line266"></a>
<a name="line267"></a>/**
<a name="line268"></a> * Generates an SQLITE CREATE TRIGGER statement from a definition array.
<a name="line269"></a> * @param {string} onStr the type of trigger to create.
<a name="line270"></a> * @param {Object} def  a schema statement definition.
<a name="line271"></a> * @param {string} notExistsStr string to be included in the create
<a name="line272"></a> *     indicating what to do.
<a name="line273"></a> * @return {string} the statement.
<a name="line274"></a> * @private
<a name="line275"></a> */
<a name="line276"></a>goog.gears.BaseStore.prototype.getCreateTriggerStatement_ =
<a name="line277"></a>    function(onStr, def, notExistsStr) {
<a name="line278"></a>  return &#39;CREATE TRIGGER &#39; + notExistsStr + def.name + &#39; &#39; +
<a name="line279"></a>          onStr + &#39; ON &#39; + def.tableName +
<a name="line280"></a>          (def.when ? (&#39; WHEN &#39; + def.when) : &#39;&#39;) +
<a name="line281"></a>          &#39; BEGIN &#39; + def.actions.join(&#39;; &#39;) + &#39;; END&#39;;
<a name="line282"></a>};
<a name="line283"></a>
<a name="line284"></a>
<a name="line285"></a>/**
<a name="line286"></a> * Generates an SQLITE CREATE statement from a definition object.
<a name="line287"></a> * @param {Object} def  a schema statement definition.
<a name="line288"></a> * @param {boolean} opt_ifNotExists true if the table or index should be
<a name="line289"></a> *     created only if it does not exist. Otherwise trying to create a table
<a name="line290"></a> *     or index that already exists will result in an exception being thrown.
<a name="line291"></a> * @return {string} the statement.
<a name="line292"></a> * @private
<a name="line293"></a> */
<a name="line294"></a>goog.gears.BaseStore.prototype.getCreateStatement_ =
<a name="line295"></a>    function(def, opt_ifNotExists) {
<a name="line296"></a>  var notExists = opt_ifNotExists ? &#39;IF NOT EXISTS &#39; : &#39;&#39;;
<a name="line297"></a>  switch (def.type) {
<a name="line298"></a>    case goog.gears.BaseStore.SchemaType.TABLE:
<a name="line299"></a>      return &#39;CREATE TABLE &#39; + notExists + def.name + &#39; (\n&#39; +
<a name="line300"></a>             def.columns.join(&#39;,\n  &#39;) +
<a name="line301"></a>             &#39;)&#39;;
<a name="line302"></a>    case goog.gears.BaseStore.SchemaType.VIRTUAL_TABLE:
<a name="line303"></a>      return &#39;CREATE VIRTUAL TABLE &#39; + notExists + def.name +
<a name="line304"></a>             &#39; USING FTS2 (\n&#39; + def.columns.join(&#39;,\n  &#39;) + &#39;)&#39;;
<a name="line305"></a>    case goog.gears.BaseStore.SchemaType.INDEX:
<a name="line306"></a>      return &#39;CREATE&#39; + (def.isUnique ? &#39; UNIQUE&#39; : &#39;&#39;) +
<a name="line307"></a>             &#39; INDEX &#39; + notExists + def.name + &#39; ON &#39; +
<a name="line308"></a>             def.tableName + &#39; (\n&#39; + def.columns.join(&#39;,\n  &#39;) + &#39;)&#39;;
<a name="line309"></a>    case goog.gears.BaseStore.SchemaType.BEFORE_INSERT_TRIGGER:
<a name="line310"></a>      return this.getCreateTriggerStatement_(&#39;BEFORE INSERT&#39;, def, notExists);
<a name="line311"></a>    case goog.gears.BaseStore.SchemaType.AFTER_INSERT_TRIGGER:
<a name="line312"></a>      return this.getCreateTriggerStatement_(&#39;AFTER INSERT&#39;, def, notExists);
<a name="line313"></a>    case goog.gears.BaseStore.SchemaType.BEFORE_UPDATE_TRIGGER:
<a name="line314"></a>      return this.getCreateTriggerStatement_(&#39;BEFORE UPDATE&#39;, def, notExists);
<a name="line315"></a>    case goog.gears.BaseStore.SchemaType.AFTER_UPDATE_TRIGGER:
<a name="line316"></a>      return this.getCreateTriggerStatement_(&#39;AFTER UPDATE&#39;, def, notExists);
<a name="line317"></a>    case goog.gears.BaseStore.SchemaType.BEFORE_DELETE_TRIGGER:
<a name="line318"></a>      return this.getCreateTriggerStatement_(&#39;BEFORE DELETE&#39;, def, notExists);
<a name="line319"></a>    case goog.gears.BaseStore.SchemaType.AFTER_DELETE_TRIGGER:
<a name="line320"></a>      return this.getCreateTriggerStatement_(&#39;AFTER DELETE&#39;, def, notExists);
<a name="line321"></a>  }
<a name="line322"></a>  return &#39;&#39;;
<a name="line323"></a>};
<a name="line324"></a>
<a name="line325"></a>
<a name="line326"></a>/**
<a name="line327"></a> * Generates an SQLITE DROP statement from a definition array.
<a name="line328"></a> * @param {Object} def  a schema statement definition.
<a name="line329"></a> * @return {string} the statement.
<a name="line330"></a> * @private
<a name="line331"></a> */
<a name="line332"></a>goog.gears.BaseStore.prototype.getDropStatement_ = function(def) {
<a name="line333"></a>  switch (def.type) {
<a name="line334"></a>    case goog.gears.BaseStore.SchemaType.TABLE:
<a name="line335"></a>    case goog.gears.BaseStore.SchemaType.VIRTUAL_TABLE:
<a name="line336"></a>      return &#39;DROP TABLE IF EXISTS &#39; + def.name;
<a name="line337"></a>    case goog.gears.BaseStore.SchemaType.INDEX:
<a name="line338"></a>      return &#39;DROP INDEX IF EXISTS &#39; + def.name;
<a name="line339"></a>    case goog.gears.BaseStore.SchemaType.BEFORE_INSERT_TRIGGER:
<a name="line340"></a>    case goog.gears.BaseStore.SchemaType.AFTER_INSERT_TRIGGER:
<a name="line341"></a>    case goog.gears.BaseStore.SchemaType.BEFORE_UPDATE_TRIGGER:
<a name="line342"></a>    case goog.gears.BaseStore.SchemaType.AFTER_UPDATE_TRIGGER:
<a name="line343"></a>    case goog.gears.BaseStore.SchemaType.BEFORE_DELETE_TRIGGER:
<a name="line344"></a>    case goog.gears.BaseStore.SchemaType.AFTER_DELETE_TRIGGER:
<a name="line345"></a>      return &#39;DROP TRIGGER IF EXISTS &#39; + def.name;
<a name="line346"></a>  }
<a name="line347"></a>  return &#39;&#39;;
<a name="line348"></a>};
<a name="line349"></a>
<a name="line350"></a>
<a name="line351"></a>/**
<a name="line352"></a> * Creates tables and indicies in the target database.
<a name="line353"></a> *
<a name="line354"></a> * @param {Array} defs  definition arrays. This is an array of objects
<a name="line355"></a> *    where each object describes a database object to create and drop.
<a name="line356"></a> *    each object contains a &#39;type&#39; field which of type
<a name="line357"></a> *    goog.gears.BaseStore.SchemaType. Each object also contains a
<a name="line358"></a> *    &#39;name&#39; which contains the name of the object to create.
<a name="line359"></a> *    A table object contains a &#39;columns&#39; field which is an array
<a name="line360"></a> *       that contains the column definitions for the table.
<a name="line361"></a> *    A virtual table object contains c &#39;columns&#39; field which contains
<a name="line362"></a> *       the name of the columns. They are assumed to be of type text.
<a name="line363"></a> *    An index object contains a &#39;tableName&#39; field which is the name
<a name="line364"></a> *       of the table that the index is on. It contains an &#39;isUnique&#39;
<a name="line365"></a> *       field which is a boolean indicating whether the index is
<a name="line366"></a> *       unqiue or not. It also contains a &#39;columns&#39; field which is
<a name="line367"></a> *       an array that contains the columns names (possibly along with the
<a name="line368"></a> *       ordering) that form the index.
<a name="line369"></a> *    The trigger objects contain a &#39;tableName&#39; field indicating the
<a name="line370"></a> *       table the trigger is on. The type indicates the type of trigger.
<a name="line371"></a> *       The trigger object may include a &#39;when&#39; field which contains
<a name="line372"></a> *       the when clause for the trigger. The trigger object also contains
<a name="line373"></a> *       an &#39;actions&#39; field which is an array of strings containing
<a name="line374"></a> *       the actions for this trigger.
<a name="line375"></a> * @param {boolean} opt_ifNotExists true if the table or index should be
<a name="line376"></a> *     created only if it does not exist. Otherwise trying to create a table
<a name="line377"></a> *     or index that already exists will result in an exception being thrown.
<a name="line378"></a> */
<a name="line379"></a>goog.gears.BaseStore.prototype.createSchema = function(defs, opt_ifNotExists) {
<a name="line380"></a>  this.database_.begin();
<a name="line381"></a>  try {
<a name="line382"></a>    for (var i = 0; i &lt; defs.length; ++i) {
<a name="line383"></a>      var sql = this.getCreateStatement_(defs[i], opt_ifNotExists);
<a name="line384"></a>      this.database_.execute(sql);
<a name="line385"></a>    }
<a name="line386"></a>    this.database_.commit();
<a name="line387"></a>  } catch (ex) {
<a name="line388"></a>    this.database_.rollback(ex);
<a name="line389"></a>  }
<a name="line390"></a>};
<a name="line391"></a>
<a name="line392"></a>
<a name="line393"></a>/**
<a name="line394"></a> * Drops tables and indicies in a target database.
<a name="line395"></a> *
<a name="line396"></a> * @param {Array} defs Definition arrays.
<a name="line397"></a> */
<a name="line398"></a>goog.gears.BaseStore.prototype.dropSchema = function(defs) {
<a name="line399"></a>  this.database_.begin();
<a name="line400"></a>  try {
<a name="line401"></a>    for (var i = defs.length - 1; i &gt;= 0; --i) {
<a name="line402"></a>      this.database_.execute(this.getDropStatement_(defs[i]));
<a name="line403"></a>    }
<a name="line404"></a>    this.database_.commit();
<a name="line405"></a>  } catch (ex) {
<a name="line406"></a>    this.database_.rollback(ex);
<a name="line407"></a>  }
<a name="line408"></a>};
<a name="line409"></a>
<a name="line410"></a>
<a name="line411"></a>/**
<a name="line412"></a> * Creates triggers specified in definitions. Will first attempt
<a name="line413"></a> * to drop the trigger with this name first.
<a name="line414"></a> *
<a name="line415"></a> * @param {Array} defs Definition arrays.
<a name="line416"></a> */
<a name="line417"></a>goog.gears.BaseStore.prototype.createTriggers = function(defs) {
<a name="line418"></a>  this.database_.begin();
<a name="line419"></a>  try {
<a name="line420"></a>    for (var i = 0; i &lt; defs.length; i++) {
<a name="line421"></a>      var def = defs[i];
<a name="line422"></a>      switch (def.type) {
<a name="line423"></a>        case goog.gears.BaseStore.SchemaType.BEFORE_INSERT_TRIGGER:
<a name="line424"></a>        case goog.gears.BaseStore.SchemaType.AFTER_INSERT_TRIGGER:
<a name="line425"></a>        case goog.gears.BaseStore.SchemaType.BEFORE_UPDATE_TRIGGER:
<a name="line426"></a>        case goog.gears.BaseStore.SchemaType.AFTER_UPDATE_TRIGGER:
<a name="line427"></a>        case goog.gears.BaseStore.SchemaType.BEFORE_DELETE_TRIGGER:
<a name="line428"></a>        case goog.gears.BaseStore.SchemaType.AFTER_DELETE_TRIGGER:
<a name="line429"></a>          this.database_.execute(&#39;DROP TRIGGER IF EXISTS &#39; + def.name);
<a name="line430"></a>          this.database_.execute(this.getCreateStatement_(def));
<a name="line431"></a>          break;
<a name="line432"></a>      }
<a name="line433"></a>    }
<a name="line434"></a>    this.database_.commit();
<a name="line435"></a>  } catch (ex) {
<a name="line436"></a>    this.database_.rollback(ex);
<a name="line437"></a>  }
<a name="line438"></a>};
<a name="line439"></a>
<a name="line440"></a>
<a name="line441"></a>/**
<a name="line442"></a> * Returns true if the table exists in the database
<a name="line443"></a> *
<a name="line444"></a> * @param {string} name The table name.
<a name="line445"></a> * @return {boolean} Whether the table exists in the database.
<a name="line446"></a> */
<a name="line447"></a>goog.gears.BaseStore.prototype.hasTable = function(name) {
<a name="line448"></a>  return this.hasInSchema_(&#39;table&#39;, name);
<a name="line449"></a>};
<a name="line450"></a>
<a name="line451"></a>
<a name="line452"></a>/**
<a name="line453"></a> * Returns true if the index exists in the database
<a name="line454"></a> *
<a name="line455"></a> * @param {string} name The index name.
<a name="line456"></a> * @return {boolean} Whether the index exists in the database.
<a name="line457"></a> */
<a name="line458"></a>goog.gears.BaseStore.prototype.hasIndex = function(name) {
<a name="line459"></a>  return this.hasInSchema_(&#39;index&#39;, name);
<a name="line460"></a>};
<a name="line461"></a>
<a name="line462"></a>
<a name="line463"></a>/**
<a name="line464"></a> * @param {string} name The name of the trigger.
<a name="line465"></a> * @return {boolean} Whether the schema contains a trigger with the given name.
<a name="line466"></a> */
<a name="line467"></a>goog.gears.BaseStore.prototype.hasTrigger = function(name) {
<a name="line468"></a>  return this.hasInSchema_(&#39;trigger&#39;, name);
<a name="line469"></a>};
<a name="line470"></a>
<a name="line471"></a>
<a name="line472"></a>/**
<a name="line473"></a> * Returns true if the database contains the index or table
<a name="line474"></a> *
<a name="line475"></a> * @private
<a name="line476"></a> * @param {string} type The type of object to test for, &#39;table&#39; or &#39;index&#39;.
<a name="line477"></a> * @param {string} name The table or index name.
<a name="line478"></a> * @return {boolean} Whether the database contains the index or table.
<a name="line479"></a> */
<a name="line480"></a>goog.gears.BaseStore.prototype.hasInSchema_ = function(type, name) {
<a name="line481"></a>  return this.database_.queryValue(&#39;SELECT 1 FROM SQLITE_MASTER &#39; +
<a name="line482"></a>      &#39;WHERE TYPE=? AND NAME=?&#39;,
<a name="line483"></a>      type,
<a name="line484"></a>      name) != null;
<a name="line485"></a>};
<a name="line486"></a>
<a name="line487"></a>
<a name="line488"></a>/**
<a name="line489"></a> * Disposes of the object.
<a name="line490"></a> */
<a name="line491"></a>goog.gears.BaseStore.prototype.disposeInternal = function() {
<a name="line492"></a>  goog.gears.BaseStore.superClass_.disposeInternal.call(this);
<a name="line493"></a>  this.database_ = null;
<a name="line494"></a>};
<a name="line495"></a>
<a name="line496"></a>
<a name="line497"></a>
<a name="line498"></a>/**
<a name="line499"></a> * HACK: The JSCompiler check for undefined properties sees that these
<a name="line500"></a> * fields are never set and raises warnings.
<a name="line501"></a> * @type {Array.&lt;Object&gt;}
<a name="line502"></a> * @private
<a name="line503"></a> */
<a name="line504"></a>goog.gears.schemaDefDummy_ = [
<a name="line505"></a>  {
<a name="line506"></a>    type: &#39;&#39;,
<a name="line507"></a>    name: &#39;&#39;,
<a name="line508"></a>    when: &#39;&#39;,
<a name="line509"></a>    tableName: &#39;&#39;,
<a name="line510"></a>    actions: [],
<a name="line511"></a>    isUnique: false
<a name="line512"></a>  }
<a name="line513"></a>];
</pre>


</body>
</html>
